<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<!-- <uni-easyinput placeholder="项目流水表ID,用于查询log_project中对应项目的信息" v-model="formData.log_project_id"></uni-easyinput> -->
			<uni-forms-item name="project_id" label="项目名称" required>
				<uni-data-picker v-model="formData.project_id" collection="sys-project-categories"
					field="name as text,_id as value" @change="onchange" @nodeclick="onnodeclick"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="place_id" label="门店信息" required>
				<uni-data-picker v-model="formData.place_id" collection="mer_place_time"
					field="name as text,_id as value"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="date" label="项目时间" required>
				<uni-datetime-picker type="datetimerange" :multiple="true"
					v-model="formData.date"></uni-datetime-picker>
			</uni-forms-item>
			<uni-forms-item name="status" label="抢单状态">
				<uni-data-checkbox v-model="formData.status"
					:localdata="formOptions.status_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="all_physios" label="指定抢单人员">
				<!-- 这个有点厉害，可以直接查询数据库-->
				<uni-data-checkbox ref="udc" :multiple="true" v-model="formData.all_physios" collection="mer-physios"
					:where="sWhere" field="nickname as text ,  _id as value" @change="onchange" @nodeclick="onnodeclick"
					loadtime="onready"></uni-data-checkbox>
			</uni-forms-item>
			<!-- <uni-forms-item name="winner" label="抢单康复师(不指定则抢单模式)">
				<uni-data-picker v-model="formData.winner" collection="mer-physios" field="nickname as text ,  _id as value"
					trim="both"></uni-data-picker>
			</uni-forms-item> -->
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回上级</button>
				</navigator>
				<button class="uni-button" style="margin-left: 15px;width: 100px;" @click="return_Category">返回目录</button>
			</view>

			<!-- <uni-forms-item name="date" label="项目时间">
				<label>{{projectChecked}}</label>
			</uni-forms-item> -->
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/log_project.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'log_project';
	const dbOrderName = 'log_pay_order';

	/* let orderData = {
		"user_id": ",",
		"assigned_physios_id": "",
		"log_project_id": "", //项目流水表主键，先插入项目流水后，应该能返回来
		"project_id": "", //等于formData中project_id
		"date": [],
		"status": 0,
	} */
	let orderDatas = []
	let that = this;


	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			//let projectChecked22 = '云中子';
			let formData = {
				"project_id": "",
				"place_id": "",
				"date": [],
				"status": 0,
				"all_physios": [],
				"winner": ""
			}
			return {
				formData,
				projectChecked: "65f9adc655b3372a1fa5ffdf",
				sWhere: '',
				formOptions: {
					"status_localdata": [{
							"text": "可抢单",
							"value": 0
						},
						{
							"text": "已被抢",
							"value": 1
						}
					]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady(e) {
			this.$refs.form.setRules(this.rules)
			
			// id = this.tempstr 且 create_time > 
			//this.sWhere = "talent=='" + this.projectChecked + "'"
		},
		onLoad(e) {
			this.formData.project_id = e.id
			if (e.id) {
				console.log(e)
				const id = e.id
				this.sWhere = "talent=='" + id + "'"
			}
			/* if (e.id) {
			  const id = e.id 
			} */
			// id = this.tempstr 且 create_time > 
			//this.sWhere = "talent=='" + this.projectChecked + "'"
			//this.sWhere = "";
			// id = this.tempstr 或 name != null
			// this.sWhere = "id=='" + this.tempstr + "' || name != null"

			// 组件上配置了 loadtime = "manual", 这里需要手动加载数据
			/* this.$nextTick(() => {
			  this.$refs.udb.loadData()
			}) */
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					console.log(res)
					console.log("测试1")
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},
			
			/**
			 * 返回目录页
			 */
			return_Category(){
				let url = './list'
				uni.navigateTo({
					url
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				//可以使用事务，我还没研究
				//db.collection(dbOrderName).add()

				return db.collection(dbCollectionName).add(value).then((res) => {
					console.log(res);
					console.log(res.result.id);

					//判断选择了几个康复师
					for (var i = 0; i < value.all_physios.length; i++) {
						var orderData = {
							"user_id": ",",
							"assigned_physios_id": "",
							"log_project_id": "", //项目流水表主键，先插入项目流水后，应该能返回来
							"project_id": "", //等于formData中project_id
							"date": [],
							"status": 0,
						}
						orderData.log_project_id = res.result.id;
						orderData.project_id = value.project_id;
						orderData.date = value.date;
						orderData.assigned_physios_id = value.all_physios[i];
						orderDatas.push(orderData)
					};
					console.log(orderDatas)

					db.collection(dbOrderName).add(orderDatas).then((res) => {
						console.log(res)
					}).catch((err) => {
						uni.showModal({
							content: err.message || '请求服务失败',
							showCancel: false
						})
					});

					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},

			onchange(e) {
				console.log(e)
				const value = e.detail.value
				console.log(value)
			},
			onnodeclick(node) {
				this.projectChecked = node.value
				let url = './add?id='+node.value
				console.log(url);
				/* this.getOpenerEventChannel().emit('refreshData')
				uni.navigateBack({
					delta: 2
				}); */
				/* console.log(this.$refs.udc)
				this.$refs.udc.loadData({
					talent: node.value
				})
				console.log(node) */
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>